﻿using System;
using System.Text;
using System.Windows.Forms;

namespace Zove.Desktop
{
    public partial class AddModelDialog : Form
    {
        public AddModelDialog()
        {
            InitializeComponent();
        }

        public AddModelDialog(string selectedPath)
            : this()
        {
            this.tbDirectory.Enabled = false;
            this.Directory = selectedPath;
        }

        public string ModelName
        {
            get
            {
                return this.tbName.Text;
            }
        }

        public string Model
        {
            get
            {
                StringBuilder buff = new StringBuilder();

                if (!string.IsNullOrEmpty(this.tbNamespace.Text))
                {
                    buff.Append("namespace ").Append(this.tbNamespace.Text).Append(";\n\n");
                }

                if(!string.IsNullOrEmpty(this.tbComment.Text)) {
                    buff.Append("/** "  +this.tbComment.Text + " */");
                }

                if (string.IsNullOrEmpty(this.tbName.Text))
                {
                    throw new Exception("Class name is empty");
                } else {
                    buff.Append("class ").Append(this.tbName.Text);
                }

                if(!string.IsNullOrEmpty(this.tbBaseClass.Text))
                {
                    buff.Append(" extends ").Append(this.tbBaseClass.Text);
                }

                buff.Append("\n{\n}");

                return buff.ToString();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.tbName.Text))
            {
                MessageBox.Show("Please enter the model name.");
                return;
            }

            if (string.IsNullOrEmpty(this.tbDirectory.Text))
            {
                MessageBox.Show("Please specify a directory to store the file.");
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        public string Directory
        {
            get
            {
                return this.tbDirectory.Text;
            }
            set
            {
                this.tbDirectory.Text = value;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnBrowseBaseClass_Click(object sender, EventArgs e) {
            OpenFileDialog d = new OpenFileDialog();

            if (d.ShowDialog() == DialogResult.OK) {
                this.tbBaseClass.Text = d.FileName;
            }
        }

        private void btnBrowseDirectory_Click(object sender, EventArgs e) {
            FolderBrowserDialog d = new FolderBrowserDialog();

            d.Description = "Select a folder to save the file.";
            if (d.ShowDialog() == DialogResult.OK) {
                this.tbDirectory.Text = d.SelectedPath;
            }
        }
    }
}
